package phone.example.com.a36kr.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.adapter.newslistviewadapter.NewsAdapter;
import phone.example.com.a36kr.bean.NewsBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.tools.BaseActivity;
import phone.example.com.a36kr.tools.MyApp;

/**
 * Created by dllo on 16/8/24.
 */
public class SearchActivity extends BaseActivity {
    private String urlFront = "https://rong.36kr.com/api/mobi/news/search?keyword=";
    private String urlRear = "&page=1&pageSize=3";
    private String sContent; // 搜索关键字
    private ListView lv;// 与新闻页用的一个listView
    protected NetTool mNetTool;
    private EditText searchEt;
    private NewsAdapter adapter;
    private String url;
    private LinearLayout backLl;
    private NewsBean newsBean;
    private View headView;
    private boolean head = false;
    private String urlS;

    @Override
    protected int setLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        searchEt = bindView(R.id.search_et);
        lv = (ListView) findViewById(R.id.search_lv);
        backLl = bindView(R.id.search_background_ll);
        headView = LayoutInflater.from(this).inflate(R.layout.search_head_more, null);
        adapter = new NewsAdapter(this);
        Button searchStartBtn = bindView(R.id.search_start_btn);
        Button searchFinishBtn = bindView(R.id.search_finish_btn);



        //搜索取消
        searchFinishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //开始搜索
        searchStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sContent = searchEt.getText().toString();
                initNet();


            }

        });
                //监听EditText
                searchEt.addTextChangedListener(new TextWatcher() {



                    /**
                     *
                     *
                     * @param s     editText长度
                     * @param start
                     * @param count
                     * @param after
                     */
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        Log.d("11111", "s:" + s);

                    }

                    //editText 监听
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                        sContent = searchEt.getText().toString();
                        try {
                            urlS = URLEncoder.encode(sContent, "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        if (s.length() > 0) {
                            backLl.setVisibility(View.GONE);
                            lv.setVisibility(View.VISIBLE);
                            initNet();
                        }else {
                            backLl.setVisibility(View.VISIBLE);
                            lv.setVisibility(View.GONE);
                        }


                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        }
                });
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sContent = searchEt.getText().toString();
                Intent intent = new Intent(SearchActivity.this, SearchMoreActivity.class);
                intent.putExtra("Keywords", sContent);
                startActivity(intent);
            }
        });



    }


    @Override
    protected void initData() {

    }


    private void initNet() {
        url = urlFront + urlS + urlRear;
        mNetTool = new NetTool();
        mNetTool.getNetData(url, NewsBean.class, new NetTool.NetListener<NewsBean>() {
            @Override
            public void onSuccess(final NewsBean newsBean) {

                adapter.setNewsBean(newsBean);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(SearchActivity.this, NewsSecondActivity.class);

                        String mFeedId = new String(newsBean.getData().getData().get(position - lv.getHeaderViewsCount()).getFeedId());
                        Log.d("NewsFragment", mFeedId);
                        intent.putExtra("newsFeedId",mFeedId);
                        startActivity(intent);
                    }
                });
                if (head == false){
                    lv.addHeaderView(headView);
                    head = true;
                }
                lv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

}
